<?php

namespace App\Listeners;

use App\Events\Rank;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\DB;

class RankListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //

    }

    /**
     * Handle the event.
     *
     * @param  \App\Events\Rank  $event
     * @return void
     */
    public function handle(Rank $event)
    {
        //

        $key = $event->key.'-'.date('Y-m');

        $redis = new \Redis();
        $redis->connect('127.0.0.1');
        $redis->zIncrBy($key,$event->score,$event->uid);

        $list = $redis->zRevRange($key,0,-1);







        $redis->close();
    }

    public function rankIterator($list)
    {
        foreach ($list as $item){
            yield $item;
        }
    }


    public function updateRank($key,$list)
    {
        switch ($key){
            // 品牌点赞量
            case 'brandLikeList':
                list($year,$month) = explode('-',date('Y-m'));




                foreach ($this->rankIterator($list) as $k=> $item){
                    DB::table('firm_brand')->where('id',$item)->update([
                        'like_rank' => $k+1
                    ]);
                }
                break;
            // 管理公司表单留言量
            case 'firmForm':
                foreach ($this->rankIterator($list) as $k=> $item){
                    DB::table('firms')->where('id',$item)->update([
                        'form_rank' => $k+1
                    ]);
                }
                break;
            // 观看量最多的管理公司
            case 'firmView':
                foreach ($this->rankIterator($list) as $k=> $item){
                    DB::table('firms')->where('id',$item)->update([
                        'view_rank' => $k+1
                    ]);
                }
                break;

            // 观看量最多的课程
            case 'CourseView':
                foreach ($this->rankIterator($list) as $k=> $item){
                    DB::table('courses')->where('id',$item)->update([
                        'view_rank' => $k+1
                    ]);
                }
                break;

            // 宝典量收藏量最高
            case 'ArticleBookCollect':
                foreach ($this->rankIterator($list) as $k=> $item){
                    DB::table('articles')->where('id',$item)->update([
                        'collect_rank' => $k+1
                    ]);
                }
                break;

            // 圈子点评量最高
            case 'CircleComment':
                foreach ($this->rankIterator($list) as $k=> $item){
                    DB::table('circles')->where('id',$item)->update([
                        'comment_rank' => $k+1
                    ]);
                }
                break;

            // 咨询观看量最高
            case 'newView':
                foreach ($this->rankIterator($list) as $k=> $item){
                    DB::table('articles')->where('id',$item)->update([
                        'view_rank' => $k+1
                    ]);
                }
                break;
            // 品牌留言量最高
            case 'brandForm':
                foreach ($this->rankIterator($list) as $k=> $item){
                    DB::table('firm_brands')->where('id',$item)->update([
                        'form_rank' => $k+1
                    ]);
                }
                break;
            // 观看量最多的品牌
            case 'brandView':
                foreach ($this->rankIterator($list) as $k=> $item){
                    DB::table('firm_brands')->where('id',$item)->update([
                        'view_rank' => $k+1
                    ]);
                }
                break;
        }
    }
}
